//
//  GTCommentManager.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/27.
//

#import <UIKit/UIKit.h>
#import "../../Utils/GTScreen.h"
#import "GTCommentManager.h"
@interface GTCommentManager ()<UITextViewDelegate>

@property (nonatomic, strong, readwrite) UIView *backgroundView;
@property (nonatomic, strong, readwrite) UITextView *textView;
- (void)_tapBackground;
- (void)_doTextViewAnimationNotification:(NSNotification *)notification;

@end

@implementation GTCommentManager

+ (GTCommentManager *)shareManager {
    static GTCommentManager *manager;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        manager = [[GTCommentManager alloc]init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];

    if (self) {
        self.backgroundView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        self.backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        [self.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(_tapBackground)]];
        [self.backgroundView addSubview:({
            self.textView = [[UITextView alloc]initWithFrame:CGRectMake(0, self.backgroundView.bounds.size.height, SCREEN_WIDTH, UI(100))];
            self.textView.backgroundColor = [UIColor whiteColor];
            self.textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
            self.textView.layer.borderWidth = 5.f;
            self.textView.delegate = self;
            self.textView;
        })];

        [[NSNotificationCenter defaultCenter]
         addObserver:self
            selector:@selector(_doTextViewAnimationNotification:)
                name:UIKeyboardWillChangeFrameNotification
              object:nil];
    }

    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - public
- (void)showCommentVIew {
    [[UIApplication sharedApplication].keyWindow addSubview:self.backgroundView];
    [self.textView becomeFirstResponder];
}

#pragma mark - private
- (void)_tapBackground {
    [self.textView resignFirstResponder];
    [self.backgroundView removeFromSuperview];
}

- (void)_doTextViewAnimationNotification:(NSNotification *)notification {
    CGFloat duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect keyboardFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    if (keyboardFrame.origin.y >= SCREEN_HEIGHT) {
        [UIView animateWithDuration:duration
                         animations:^{
            self.textView.frame = CGRectMake(0, self.backgroundView.bounds.size.height, SCREEN_WIDTH, UI(100));
        }];
    } else {
        self.textView.frame = CGRectMake(0, self.backgroundView.bounds.size.height, SCREEN_WIDTH, UI(100));
        [UIView animateWithDuration:duration
                         animations:^{
            self.textView.frame = CGRectMake(0, self.backgroundView.bounds.size.height - keyboardFrame.size.height - UI(100), SCREEN_WIDTH, UI(100));
        }];
    }
}

@end
